package com.hxzy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hxzy.dto.WmsRespositoryDTO;
import com.hxzy.entity.WmsRespository;
import com.hxzy.mapper.WmsRespositoryMapper;
import com.hxzy.service.WmsRespositoryService;
import com.hxzy.util.MybatisUtil;
import com.hxzy.vo.BootstrapTableVO;
import com.hxzy.vo.ResultEnum;
import com.hxzy.vo.ResultVO;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class WmsRespositoryServiceImpl implements WmsRespositoryService {
    @Override
    public boolean insert(WmsRespository record) {
        int count=0;
        try (SqlSession session= MybatisUtil.getSqlSessionFactory().openSession()){
            WmsRespositoryMapper mapper = session.getMapper(WmsRespositoryMapper.class);
            count=mapper.insert(record);
            session.commit();
        }
        return count>0;
    }

    @Override
    public BootstrapTableVO search(WmsRespositoryDTO dto) {
        BootstrapTableVO tableVO=new BootstrapTableVO();
        try (SqlSession session=MybatisUtil.getSqlSessionFactory().openSession()){
            WmsRespositoryMapper mapper = session.getMapper(WmsRespositoryMapper.class);
            PageHelper.startPage(dto.getPageNumber(),dto.getPageSize());
            List<WmsRespository> search = mapper.search(dto);

            PageInfo<WmsRespository> pg=new PageInfo<>(search);
            tableVO.setTotal(pg.getTotal());
            tableVO.setRows(search);
        }
        return tableVO;
    }

    @Override
    public ResultVO<String> upDeleteByPrimaryKey(Integer id) {
        int count=0;
        try (SqlSession session=MybatisUtil.getSqlSessionFactory().openSession()){
            WmsRespositoryMapper mapper = session.getMapper(WmsRespositoryMapper.class);
           count= mapper.upDeleteByPrimaryKey(id);
           session.commit();
        }
        ResultVO<String> vo = null;
        if (count>0){
            vo = ResultVO.success("删除成功");
        }else {
            vo = ResultVO.failed(ResultEnum.DB_DELETE_FAILED);
        }
        return vo;
    }

    @Override
    public boolean updateByPrimaryKey(WmsRespository record) {
        int count=0;
        try (SqlSession session=MybatisUtil.getSqlSessionFactory().openSession()){
            WmsRespositoryMapper mapper = session.getMapper(WmsRespositoryMapper.class);
            count=mapper.updateByPrimaryKey(record);
            session.commit();
        }
        return count>0;
    }

    @Override
    public WmsRespository selectByPrimaryKey(Integer id) {
        WmsRespository wmsRespository=null;
        try (SqlSession session=MybatisUtil.getSqlSessionFactory().openSession()){
            WmsRespositoryMapper mapper = session.getMapper(WmsRespositoryMapper.class);
            wmsRespository=mapper.selectByPrimaryKey(id);
        }
        return wmsRespository;
    }

    @Override
    public List<WmsRespository> selectAll() {
        List<WmsRespository> list=null;
        try (SqlSession session=MybatisUtil.getSqlSessionFactory().openSession()){
            WmsRespositoryMapper mapper = session.getMapper(WmsRespositoryMapper.class);
           list= mapper.selectAll();
        }
        return list;
    }
}
